import json;


def getNewData(data_matrix,set):
    '''
    :param data_matrix: 待处理数组
    :param set: 待处理数组中需处理数据下标
    :return: 新数组
    '''
    new_data = [];
    for i in range(len(data_matrix)):
        new_data.append(data_matrix[i][set]);
    return new_data



# 打开文本文件
fp = open(r'NOAA_mod.txt', 'r');

# 使用readlines读取
lines = fp.readlines();

coyote_creek = {};
santa_monica = {};

#定义列表存放各类型数据
timeList = [];
c_h2o_feet = {"datas":[]};
s_h2o_feet = {"datas":[]};
c_h2o_quality = {"datas":[]};
s_h2o_quality = {"datas":[]};
c_h2o_pH = {"datas":[]};
s_h2o_pH = {"datas":[]};
c_average_temperature = {"datas":[]};
s_average_temperature = {"datas":[]};
c_h2o_temperature = {"datas":[]};
s_h2o_temperature = {"datas":[]};



# 定义字典综合所有数据
dataDict = {};

for line in lines:
    dataList = line.split(",");
    dataKind = dataList[0]
    if dataKind == "h2o_feet":
        # 获取数据
        conList = dataList[1].split();
        localtion = conList[0].split("=")[1];
        time = int(str(conList[2])+'000');
        msg = float(conList[1].split("=")[1]);
        if localtion == "coyote_creek" :
            c_h2o_feet["datas"].append([time,msg]);
        elif localtion == "santa_monica" :
            s_h2o_feet["datas"].append([time,msg]);
    elif dataKind == "h2o_quality":
        conList = dataList[2].split();
        localtion = dataList[1].split("=")[1];
        time = int(str(conList[2])+'000');
        randtag = conList[0].split("=")[1];
        index = float(conList[1].split("=")[1]);
        # msgDict = {"randtag":randtag,"index":index};
        if localtion == "coyote_creek" :
            c_h2o_quality["datas"].append([time,index]);
        elif localtion == "santa_monica" :
            s_h2o_quality["datas"].append([time,index]);
    elif dataKind == "h2o_pH":
        conList = dataList[1].split();
        localtion = conList[0].split("=")[1];
        time = int(str(conList[2])+'000');
        msg = float(conList[1].split("=")[1]);
        if localtion == "coyote_creek" :
            c_h2o_pH["datas"].append([time,msg]);
        elif localtion == "santa_monica" :
            s_h2o_pH["datas"].append([time,msg]);
    elif dataKind == "average_temperature":
        conList = dataList[1].split();
        localtion = conList[0].split("=")[1];
        time = int(str(conList[2])+'000');
        msg = float(conList[1].split("=")[1]);
        if localtion == "coyote_creek" :
            c_average_temperature["datas"].append([time,msg]);
        elif localtion == "santa_monica" :
            s_average_temperature["datas"].append([time,msg]);
    elif dataKind == "h2o_temperature":
        conList = dataList[1].split();
        localtion = conList[0].split("=")[1];
        time = int(str(conList[2])+'000');
        msg = float(conList[1].split("=")[1]);
        if localtion == "coyote_creek" :
            c_h2o_temperature["datas"].append([time,msg]);
        elif localtion == "santa_monica" :
            s_h2o_temperature["datas"].append([time,msg]);

# 把所有的数据存放到数据字典里
dataDict["coyote_creek"] = {"h2o_feet":c_h2o_feet,"h2o_quality":c_h2o_quality,"h2o_pH":c_h2o_pH,"average_temperature":c_average_temperature,"h2o_temperature":c_h2o_temperature}
dataDict["santa_monica"] = {"h2o_feet":s_h2o_feet,"h2o_quality":s_h2o_quality,"h2o_pH":s_h2o_pH,"average_temperature":s_average_temperature,"h2o_temperature":s_h2o_temperature}

#求各组数据平均值、最大值最小值
    # coyote_creek
        # h2o_feet
newData = getNewData(dataDict["coyote_creek"]["h2o_feet"]["datas"],1);
dataDict["coyote_creek"]["h2o_feet"]["max"] = max(newData);
dataDict["coyote_creek"]["h2o_feet"]["min"] = min(newData);
dataDict["coyote_creek"]["h2o_feet"]["avg"] = sum(newData)/len(newData);
        # h2o_quality
newData = getNewData(dataDict["coyote_creek"]["h2o_quality"]["datas"],1);
dataDict["coyote_creek"]["h2o_quality"]["max"] = max(newData);
dataDict["coyote_creek"]["h2o_quality"]["min"] = min(newData);
dataDict["coyote_creek"]["h2o_quality"]["avg"] = sum(newData)/len(newData);
        # h2o_pH
newData = getNewData(dataDict["coyote_creek"]["h2o_pH"]["datas"],1);
dataDict["coyote_creek"]["h2o_pH"]["max"] = max(newData);
dataDict["coyote_creek"]["h2o_pH"]["min"] = min(newData);
dataDict["coyote_creek"]["h2o_pH"]["avg"] = sum(newData)/len(newData);
        # average_temperature
newData = getNewData(dataDict["coyote_creek"]["average_temperature"]["datas"],1);
dataDict["coyote_creek"]["average_temperature"]["max"] = max(newData);
dataDict["coyote_creek"]["average_temperature"]["min"] = min(newData);
dataDict["coyote_creek"]["average_temperature"]["avg"] = sum(newData)/len(newData);
        # h2o_temperature
newData = getNewData(dataDict["coyote_creek"]["h2o_temperature"]["datas"],1);
dataDict["coyote_creek"]["h2o_temperature"]["max"] = max(newData);
dataDict["coyote_creek"]["h2o_temperature"]["min"] = min(newData);
dataDict["coyote_creek"]["h2o_temperature"]["avg"] = sum(newData)/len(newData);

    # santa_monica
        # h2o_feet
newData = getNewData(dataDict["santa_monica"]["h2o_feet"]["datas"],1);
dataDict["santa_monica"]["h2o_feet"]["max"] = max(newData);
dataDict["santa_monica"]["h2o_feet"]["min"] = min(newData);
dataDict["santa_monica"]["h2o_feet"]["avg"] = sum(newData)/len(newData);
        # h2o_quality
newData = getNewData(dataDict["santa_monica"]["h2o_quality"]["datas"],1);
dataDict["santa_monica"]["h2o_quality"]["max"] = max(newData);
dataDict["santa_monica"]["h2o_quality"]["min"] = min(newData);
dataDict["santa_monica"]["h2o_quality"]["avg"] = sum(newData)/len(newData);
        # h2o_pH
newData = getNewData(dataDict["santa_monica"]["h2o_pH"]["datas"],1);
dataDict["santa_monica"]["h2o_pH"]["max"] = max(newData);
dataDict["santa_monica"]["h2o_pH"]["min"] = min(newData);
dataDict["santa_monica"]["h2o_pH"]["avg"] = sum(newData)/len(newData);
        # average_temperature
newData = getNewData(dataDict["santa_monica"]["average_temperature"]["datas"],1);
dataDict["santa_monica"]["average_temperature"]["max"] = max(newData);
dataDict["santa_monica"]["average_temperature"]["min"] = min(newData);
dataDict["santa_monica"]["average_temperature"]["avg"] = sum(newData)/len(newData);
        # h2o_temperature
newData = getNewData(dataDict["santa_monica"]["h2o_temperature"]["datas"],1);
dataDict["santa_monica"]["h2o_temperature"]["max"] = max(newData);
dataDict["santa_monica"]["h2o_temperature"]["min"] = min(newData);
dataDict["santa_monica"]["h2o_temperature"]["avg"] = sum(newData)/len(newData);

# 把字典转化成json并写入json文件
with open('data.json', 'w') as f:
    f.write(json.dumps(dataDict))
